package utils;

import java.net.InetAddress;
import java.util.Random;

import com.et.ar.exception.ActiveRecordException;

import rbac.models.Config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Demandinfo;

public class ConfigUtil {


  public static List getConfigItems(String cfgtype)
     throws Exception
   {
     List<Object> items = new ArrayList<Object>();
 
     List<Config> configs= Config.findAll(Config.class, "cfgtype=?", new Object[]{ cfgtype }, "id");
     for (Config config : configs) {
       Map<String,String> item = new HashMap<String,String>();
       item.put("id", config.getCfgkeyname());
       item.put("text", config.getCfgkeyname());
       items.add(item);
     }
 
     return items;
   }
   
	// 根据配置编码获取配置值
	public static String getConfig(String code) {
		Config cfg;
		try {
			cfg = Config.findFirst(Config.class, "cfgcode=?",
					new Object[] { code });

			if (cfg == null) {
				return "";
			} else {
				return cfg.getCfgkeyvalue();
			}
		} catch (ActiveRecordException e) {
			e.printStackTrace();
			return "";
		}

	}

	public static String getPath(String code) {
		String path = getConfig(code);
		// path = path.replace("\\", java.io.File.separator);
		return path;
	}

	// 随机返回取得内容中以,隔开的多个子串中的一个
	public static String getRandomString(String code) {
		String[] cpus = getConfig(code).trim().split(",");
		if (cpus.length == 0)
			return null;
		Random r = new Random();
		int v = Math.abs(r.nextInt(cpus.length));
		String tempstr = cpus[v];
		if (tempstr.trim().equals(""))
			return null;
		return tempstr;
	}

	

}
